import os
import time
from selenium.webdriver.common.by import By
from common.base import Base
from common.dataoperate import DataOperate
from common.functions import open_browser


# 配置文件路径
project_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# print(project_path)
path = os.path.join(project_path, "data/address.csv")
# print(path)
address_list = DataOperate(path).get_data_to_dict()
# print(account_list)


class AddressPage(Base):
    def a_click(self):
        # 定位收货地址并点击
        self.click((By.XPATH, "//a[4]"))

    # 循环增加收货地址
    def add_address(self):
        i = 0
        while i < 3:
            # 定位下拉选项并通过文本选择选项(选择省)
            self.select_by_text((By.ID, f"selProvinces_{i}"), address_list[i]["text"])
            time.sleep(2)
            # 定位下拉选项并通过文本选择选项(选择市)
            self.select_by_text((By.ID, f"selCities_{i}"), address_list[i]["text1"])
            time.sleep(2)
            # 定位下拉选项并通过文本选择选项(选择区)
            self.select_by_text((By.ID, f"selDistricts_{i}"), address_list[i]["text2"])
            # 通过ID定位输入框并输入内容
            self.send_keys((By.ID, f"consignee_{i}"), address_list[i]["name"])
            self.send_keys((By.ID, f"address_{i}"), address_list[i]["addr"])
            self.send_keys((By.ID, f"tel_{i}"), address_list[i]["tel"])
            self.clear((By.ID, f"email_{i}"))
            self.send_keys((By.ID, f"email_{i}"), address_list[i]["email"])
            self.send_keys((By.ID, f"zipcode_{i}"), address_list[i]["postcode"])
            self.send_keys((By.ID, f"mobile_{i}"), address_list[i]["phone"])
            # 点击新增收货地址按钮
            self.click((By.XPATH, "//input[@value='新增收货地址']"))
            time.sleep(3)
            i += 1

    def del_address(self):
        # 删除收货地址
        # 定位删除按钮并点击
        self.click((By.XPATH, "//body/div[6]/div[2]/div/div/div/form[2]/table/tbody/tr[5]/td[2]/input[2]"))
        time.sleep(1)
        # 接受弹框的确定
        self.alert()
        # 删除收货地址
        # 定位删除按钮并点击
        self.click((By.XPATH, "//body/div[6]/div[2]/div/div/div/form[2]/table/tbody/tr[5]/td[2]/input[2]"))
        time.sleep(1)
        # 接受弹框的确定
        self.alert()
        # 定位删除按钮并点击
        self.click((By.XPATH, "//body/div[6]/div[2]/div/div/div/form[1]/table/tbody/tr[5]/td[2]/input[2]"))
        # 接受弹窗的确定
        self.alert()
        time.sleep(1)

    def alter_address(self):
        # 修改收货地址
        self.select((By.ID, "selProvinces_0"), 12)
        time.sleep(2)
        self.select((By.ID, "selCities_0"), 4)
        time.sleep(2)
        self.select((By.ID, "selDistricts_0"), 3)
        # 清空编辑框
        self.clear((By.ID, "consignee_0"))
        self.send_keys((By.ID, "consignee_0"), "dd")
        # 清空编辑框
        self.clear((By.ID, "address_0"))
        self.send_keys((By.ID, "address_0"), "华中科技园华师一附中1")
        # 清空编辑框
        self.clear((By.ID, "tel_0"))
        self.send_keys((By.ID, "tel_0"), "027-13445678")
        # 清空编辑框
        self.clear((By.ID, "email_0"))
        self.send_keys((By.ID, "email_0"), "123654@qq.com")
        # 清空编辑框
        self.clear((By.ID, "zipcode_0"))
        self.send_keys((By.ID, "zipcode_0"), "410000")
        # 清空编辑框
        self.clear((By.ID, "mobile_0"))
        self.send_keys((By.ID, "mobile_0"), "15578956700")
        # 定位确认修改按钮,并点击确认
        self.click((By.XPATH, "//body/div[6]/div[2]/div/div/div/form[1]/table/tbody/tr[5]/td[2]/input[1]"))


if __name__ == '__main__':
    ad = AddressPage(open_browser())
    ad.add_address()
    ad.del_address()
    ad.alter_address()



